package com.zeny.coding.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.RoleDto;
import com.zeny.coding.entity.Role;
import com.zeny.coding.entity.RoleExample;
import com.zeny.coding.mapper.RoleMapper;
import com.zeny.coding.service.RoleService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h2>角色服务</h2>
 *
 * @Author Zeny
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void list(PageDto<RoleDto> pageDto) {
        //分页
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //设置条件
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause("id desc");
        //查询结果
        List<Role> roleList = this.roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        //设置总条数
        pageDto.setTotal(pageInfo.getTotal());
        //封装成DTO传输
        List<RoleDto> roleDtoList = new ArrayList<>();
        for (Role role : roleList) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(role, roleDto);
            roleDtoList.add(roleDto);
        }
        //设置记录数
        pageDto.setList(roleDtoList);
    }

    @Override
    public void save(RoleDto roleDto) {
        //DTO转~
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        role.setId(String.valueOf(snowFlake.nextId()));
        this.roleMapper.insertSelective(role);
    }

    @Override
    public void update(RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        this.roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public RoleDto findById(String id) {
        Role role = this.roleMapper.selectByPrimaryKey(id);
        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(role, roleDto);
        return roleDto;
    }

    @Override
    public void deleteById(String id) {
        this.roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<String, List<RoleDto>> findAssignRoleRelation(String id) {
        //根据用户id查询出已分配的角色
        List<Role> assignedRoles = this.roleMapper.findAssignedRole(id);
        //查找所有角色
        List<Role> allRoles = this.roleMapper.selectByExample(null);
        List<Role> unAssignRoles = new ArrayList<>();
        //遍历已分配的角色
        for (Role assignedRole : assignedRoles) {
            allRoles.removeIf(role -> assignedRole.getId().equals(role.getId()));
        }
        unAssignRoles = allRoles;
        List<RoleDto> assignedRolesDto = new ArrayList<>();
        List<RoleDto> unAssignRolesDto = new ArrayList<>();
        for (Role assignedRole : assignedRoles) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(assignedRole, roleDto);
            assignedRolesDto.add(roleDto);
        }
        for (Role unAssignRole : unAssignRoles) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(unAssignRole, roleDto);
            unAssignRolesDto.add(roleDto);
        }
        Map<String, List<RoleDto>> map = new HashMap<>();
        map.put(CourseConstants.UNASSIGNED_ROLES, unAssignRolesDto);
        map.put(CourseConstants.ASSIGNED_ROLES, assignedRolesDto);
        return map;
    }
}
